<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('通过swagger导入')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-swagger_import">
        <div class="select-list">
            <ul>
                <li class="col-sm-3">
                    <label style="width: 100%" class="control-label is-required">swagger url：</label>
                </li>
                <li class="col-sm-6">
                    <input name="swaggerResourcesUrl" id="swaggerResourcesUrl" style="width: 100%" type="text" value="http://127.0.0.1/swagger-resources" required>
                </li>
                <li class="col-sm-2">
                    <a class="btn btn-primary btn-rounded btn-sm" onclick="searchSwagger()"><i class="fa fa-search"></i>查询</a>
                </li>
            </ul>
        </div>
    </form>
</div>

<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-gross">
        <div class="select-list">
            <div class="form-group">
                <label class="col-sm-3 control-label">描述前缀：</label>
                <div class="col-sm-8">
                    <input name="grossDesc" id="grossDesc" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">mapping前缀：</label>
                <div class="col-sm-8">
                    <input name="grossMapping" id="grossMapping" type="text" class="form-control">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">判定表达式：</label>
                <div class="col-sm-8">
                    <textarea name="grossJudgeExpression" id="grossJudgeExpression" class="form-control">return innate().at(0).exist()</textarea>
                </div>
            </div>
        </div>
    </form>
</div>

<div class="col-sm-12 select-table table-striped">
    <table id="bootstrap-table" class="table table-bordered table-hover" style="width: 100%">
        <thead></thead>
        <tbody></tbody>
    </table>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "tester/task_interface"
    $("#form-swagger_import").validate({
        focusCleanup: true
    });

    function searchSwagger() {
        if ($.validate.form()) {
            var paramdata = {"swaggerResourcesUrl":$("#swaggerResourcesUrl").val()};
            $.ajax({
                url: prefix + "/swagger_import",
                data: paramdata,
                type: 'POST',
                dataType: 'json',
                success: function (result) {
                    $("#bootstrap-table thead").empty();
                    $("#bootstrap-table tbody").empty();
                    $("#bootstrap-table thead").append('<tr><th>接口描述</th><th>接口mapping</th><th>http方式</th><th>content_type</th><th>query参数</th><th>body参数</th><th>path参数</th><th>操作</th></tr>');
                    for(var i=0; i<result.rows.length; i++){
                        var row = result.rows[i];
                        var rowDesc = row.desc == "" ? "-" : row.desc;
                        var rowQueryParam = row.queryParam == "" ? "-" : row.queryParam;
                        var rowPathParam = row.pathParam == "" ? "-" : row.pathParam;
                        var rowBodyParam = row.bodyParam == "" ? "-" : row.bodyParam;
                        var rowMapping = row.mapping;
                        var rowHttpMethod = row.httpMethod;
                        var rowContentType = row.contentType == "" ? "-" : row.contentType;
                        $("#bootstrap-table tbody").append('<tr style="word-wrap:break-word;word-break:break-all;"><td>'+rowDesc+'</td><td>'+rowMapping+'</td><td>'+rowHttpMethod+'</td><td>'+rowContentType+'</td><td>'+rowQueryParam+'</td><td>'+rowBodyParam+'</td><td>'+rowPathParam+'</td><td onclick="removeRow(this)"><a class="btn btn-danger btn-xs " href="javascript:void(0)"><i class="fa fa-remove"></i>移除</a></td></tr>');
                    }
                }
            });
        }
    }

    function removeRow(td){
        var $td = $(td);
        console.info($td);
        $td.parent().remove();
    }

    function submitHandler() {
        if ($.validate.form()) {
            var grossDesc = $("#grossDesc").val();
            var grossMapping = $("#grossMapping").val();
            var grossJudgeExpression = $("#grossJudgeExpression").val();
            var table = [];
            var rows = $("#bootstrap-table tbody tr");
            for (var i=0; i<rows.length; i++){
                var row = {};
                row.desc = grossDesc + ($(rows[i]).children()[0].innerText == "-" ? "" : $(rows[i]).children()[0].innerText);
                row.mapping = grossMapping + $(rows[i]).children()[1].innerText;
                row.httpMethod = $(rows[i]).children()[2].innerText;
                row.contentType = $(rows[i]).children()[3].innerText == "-" ? "" : $(rows[i]).children()[3].innerText;
                row.queryParam = $(rows[i]).children()[4].innerText == "-" ? "" : $(rows[i]).children()[4].innerText;
                row.bodyParam = $(rows[i]).children()[5].innerText == "-" ? "" : $(rows[i]).children()[5].innerText;
                row.pathParam = $(rows[i]).children()[6].innerText == "-" ? "" : $(rows[i]).children()[6].innerText;
                row.judgeExpression = grossJudgeExpression;
                table.push(row);
            }
            $.ajax({
                url: prefix + "/saveSwaggerInterface",
                data: JSON.stringify(table),
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                success: function (result) {
                    console.info(result);
                    if (result.code == web_status.SUCCESS) {
                        $.modal.closeAll();
                        $.modal.alertSuccess(result.msg);
                    } else if (result.code == web_status.WARNING) {
                        $.modal.enable();
                        $.modal.alertWarning(result.msg)
                    } else {
                        $.modal.enable();
                        $.modal.alertError(result.msg);
                    }
                }
            });
        }
    }
</script>
</body>
</html>